﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Exam
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var inquire = "select * from Students";

            var dt = DbHelper.GetDataTable(inquire);
            dataGridView1.DataSource = dt;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.ReadOnly = true;
            dataGridView1.AllowUserToAddRows = false;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //查询
            var name = userName.Text;
            var sql = string.Format("select * from Students where Name like '%{0}%'",name);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //添加
            AddUpdate addUpdate = new AddUpdate();
            var res = addUpdate.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var inquire = "select * from Students";

                var dt = DbHelper.GetDataTable(inquire);

                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("请确认是否要取消添加","提示");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //更新
            var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            var name = (string)dataGridView1.SelectedRows[0].Cells["Name"].Value;
            var age = (int)dataGridView1.SelectedRows[0].Cells["Age"].Value;
            var score = (int)dataGridView1.SelectedRows[0].Cells["Score"].Value;

            AddUpdate addUpdate = new AddUpdate(id,name ,age ,score );
            var res = addUpdate.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var inquire = "select * from Students";

                var dt = DbHelper.GetDataTable(inquire);

                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("请确认是否要取消更新","提示");
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            //删除
            if (dataGridView1.SelectedRows.Count > 0)
            {

                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;

                var sql = string.Format("delete from Students where Id ={0}", id);

                var res = DbHelper.AddOrUpdate(sql);
                if (res == 1)
                {
                    MessageBox.Show("删除成功", "提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    var inquire = "select * from Students";
                    var dt = DbHelper.GetDataTable(inquire);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("删除失败","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
            }
        }
    }
}
